package net.jarang.support;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static String camelize(String str, boolean... toUpper) {
		Pattern pattern = Pattern.compile("[^_]+");
		Matcher matcher = pattern.matcher(str);
		StringBuffer buffer = new StringBuffer();
		while (matcher.find()) {
			buffer.append(matcher.group(0).substring(0, 1).toUpperCase());
			buffer.append(matcher.group(0).substring(1));
		}
		String result = buffer.toString();
		if (0 < toUpper.length && !toUpper[0])
			result = result.substring(0, 1).toLowerCase() + result.substring(1);
		return result;
	}

	public static String underscore(String str, boolean... toUpper) {
		Pattern pattern = Pattern.compile("[A-Z]+[a-z\\d]+");
		Matcher matcher = pattern.matcher(str);
		StringBuffer buffer = new StringBuffer();
		while (matcher.find()) {
			buffer.append("_");
			buffer.append(matcher.group(0).toLowerCase());
		}
		String result = buffer.toString().substring(1);
		if (0 < toUpper.length && toUpper[0])
			result = result.toUpperCase();
		return result;
	}
}
